package com.mingqijia.gassafety.authority.web.facade.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mingqijia.gassafety.authority.api.req.config.CreateParamConfigReqDTO;
import com.mingqijia.gassafety.authority.api.req.config.QueryDivisionReqDTO;
import com.mingqijia.gassafety.authority.api.req.config.QueryParamConfigReqDTO;
import com.mingqijia.gassafety.authority.api.req.config.UpdateParamConfigReqDTO;
import com.mingqijia.gassafety.authority.api.resp.config.DivisionLiteRespDTO;
import com.mingqijia.gassafety.authority.api.resp.config.ParamConfigRespDTO;
import com.mingqijia.gassafety.authority.api.web.facade.ConfigureManageFacade;
import com.mingqijia.gassafety.authority.dal.command.*;
import com.mingqijia.gassafety.authority.dal.handler.ConfigureTranslator;
import com.mingqijia.gassafety.authority.dal.handler.DivisionTranslator;
import com.mingqijia.gassafety.authority.dal.provider.ConfigureProvider;
import com.mingqijia.gassafety.authority.dal.provider.DivisionProvider;
import com.mingqijia.gassafety.db.entity.auth.DivisionEntry;
import com.mingqijia.gassafety.db.entity.auth.ParamConfigEntry;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.enums.ParamType;
import com.mingqijia.gassafety.shared.enums.RunningState;
import com.mingqijia.gassafety.shared.facade.BaseFacade;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.shared.handler.GlobalAuthenticationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 参数配置REST接口实现
 * @author Damon S.
 * @version v1.0.1
 * @date 2021年04月13日 18:42
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class ConfigureManageFacadeImpl extends BaseFacade implements ConfigureManageFacade {

    private final ConfigureProvider provider;
    private final ConfigureTranslator translator;
    private final DivisionTranslator dTranslator;
    private final DivisionProvider dProvider;


    @Override
    public PageUtil<DivisionLiteRespDTO> findDivision(@Valid QueryDivisionReqDTO req) {
        QueryDivisionCommand command = dTranslator.toCommand(req);
        Page<DivisionEntry> results = dProvider.find(command);
        return PageUtil.init(results, dTranslator.toRespDTO(results.getRecords()));
    }

    @Override
    public List<DivisionLiteRespDTO> findDivisionHierarchy() {
        return dProvider.listDivisionHierarchy();
    }

    @Override
    public List<DivisionLiteRespDTO> findByParentId(String parentId) {
        List<DivisionEntry> entries = dProvider.getDivision(parentId);
        return dTranslator.toRespDTO(entries);
    }

    @Override
    public PageUtil<ParamConfigRespDTO> findParamConfig(@Valid QueryParamConfigReqDTO req) {
        QueryParamConfigCommand command = translator.toCommand(req);
        command.setType(ParamType.CONFIG);
        // 分页查询
        Page<ParamConfigEntry> results = provider.find(command);
        return PageUtil.init(results, translator.toRespDTO(results.getRecords()));
    }

    @Override
    public String createParamConfig(@Valid CreateParamConfigReqDTO req) {
        CreateParamConfigCommand command = translator.toCommand(req);
        command.setType(ParamType.CONFIG);
        return provider.create(command);
    }

    @Override
    public void updateParamConfig(@Valid UpdateParamConfigReqDTO req) {
        if (!provider.update(translator.toCommand(req))) {
            throw new GlobalAuthenticationException("更新操作失败");
        }
    }

    @Override
    public void removeParamConfig(String configId) {
        RemoveParamConfigCommand command = RemoveParamConfigCommand.builder()
                .updatedBy(getUserBackendId()).id(configId).build();
        if (!provider.remove(command)) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }

    @Override
    public void switchParamConfig(String configId, Integer state) {
        if (state > Constants.INT_ONE || state < Constants.INT_ZERO) {
            throw new GlobalAuthenticationException("参数不合法");
        }
        UpdateParamConfigCommand command = UpdateParamConfigCommand.builder().id(configId)
                .state(RunningState.parse(state)).updatedBy(getUserBackendId()).build();
        if (!provider.update(command)) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }
}
